using System;
using System.Data;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;
using PCSComSale.Order.DS;
using PCSComUtils.DataContext;
using System.Linq;
using System.Transactions;
using PCSComUtils.DataAccess;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace PCSComSale.Order.BO
{
	public class SODeliveryScheduleBO //:  ISODeliveryScheduleBO
	{
		public SODeliveryScheduleBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IObjectBO Members

		//**************************************************************************              
		///    <Description>
		///       This method uses to update a DataSet
		///    </Description>
		///    <Inputs>
		///        DataSet       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, February 01, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		//[AutoComplete(true)]
		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  Add SODeliveryScheduleBO.UpdateDataSet implementation
			try
			{
				SO_DeliveryScheduleDS objSO_DeliveryScheduleDS = new SO_DeliveryScheduleDS();
				objSO_DeliveryScheduleDS.UpdateDataSet(dstData);

			}			
			catch (PCSDBException ex) 
			{
				throw ex;
			}
			catch (Exception ex) 
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to update a DataSet
		///    </Description>
		///    <Inputs>
		///        DataSet       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, February 01, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

        /*
         * + SO_DeliveryScheduleTable.DELIVERYSCHEDULEID_FLD + ","
					+ SO_DeliveryScheduleTable.REQUIREDDATE_FLD + ","
					+ SO_DeliveryScheduleTable.PROMISEDATE_FLD + ","
					+ SO_DeliveryScheduleTable.SCHEDULEDATE_FLD + ","
					+ SO_DeliveryScheduleTable.DELIVERYQUANTITY_FLD + ","
					//+ SO_DeliveryScheduleTable.DELIVERYNO_FLD + ","
					+ SO_DeliveryScheduleTable.LINE_FLD + ","
					+ SO_DeliveryScheduleTable.GATEID_FLD + ","
					+ SO_DeliveryScheduleTable.SALEORDERDETAILID_FLD 
         */

        public void Update(DataSet dstData, int pintSaleOrderDetailID)
        {
            const string METHOD_NAME = "" + ".Update()";
            try
            {
                using (var trans = new TransactionScope())
                {
                    var dcPCS = new PCSDataContext(Utils.Instance.ConnectionString);
                    IList<SO_DeliverySchedule> lists = new List<SO_DeliverySchedule>();
                    var listsDelete = new List<SO_DeliverySchedule>();
                    for (int i = 0; i < dstData.Tables[0].Rows.Count; i++)
                    {
                        try
                        {
                            if (dstData.Tables[0].Rows[i]["DeliveryScheduleID"] != DBNull.Value)
                            {
                                try
                                {
                                    SO_DeliverySchedule objDelete = dcPCS.SO_DeliverySchedules.SingleOrDefault(e => e.DeliveryScheduleID == Convert.ToInt32(dstData.Tables[0].Rows[i]["DeliveryScheduleID"].ToString()));
                                    if (objDelete != null)
                                    {

                                        try
                                        {
                                            objDelete.DeliveryQuantity = Convert.ToDecimal(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.DELIVERYQUANTITY_FLD].ToString());
                                        }
                                        catch
                                        { }

                                        try
                                        {
                                            objDelete.GateID = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.GATEID_FLD].ToString());
                                        }
                                        catch
                                        { }
                                        objDelete.Line = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.LINE_FLD].ToString());
                                        try
                                        {
                                            objDelete.PromiseDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.PROMISEDATE_FLD].ToString());
                                        }
                                        catch
                                        { }
                                        try
                                        {
                                            objDelete.RequiredDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.REQUIREDDATE_FLD].ToString());
                                        }
                                        catch
                                        { }
                                        objDelete.SaleOrderDetailID = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.SALEORDERDETAILID_FLD].ToString());
                                        try
                                        {
                                            objDelete.ScheduleDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.SCHEDULEDATE_FLD].ToString());
                                        }
                                        catch
                                        { }
                                    }
                                    else
                                    {
                                        try
                                        {
                                            SO_DeliverySchedule objDeliveryVO = new SO_DeliverySchedule();
                                            objDeliveryVO.DeliveryNo = null;
                                            try
                                            {
                                                objDeliveryVO.DeliveryQuantity = Convert.ToDecimal(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.DELIVERYQUANTITY_FLD].ToString());
                                            }
                                            catch
                                            { }
                                            objDeliveryVO.DeliveryScheduleID = 0;
                                            try
                                            {
                                                objDeliveryVO.GateID = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.GATEID_FLD].ToString());
                                            }
                                            catch
                                            { }
                                            objDeliveryVO.Line = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.LINE_FLD].ToString());
                                            try
                                            {
                                                objDeliveryVO.PromiseDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.PROMISEDATE_FLD].ToString());
                                            }
                                            catch
                                            { }
                                            try
                                            {
                                                objDeliveryVO.RequiredDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.REQUIREDDATE_FLD].ToString());
                                            }
                                            catch
                                            { }
                                            objDeliveryVO.SaleOrderDetailID = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.SALEORDERDETAILID_FLD].ToString());
                                            try
                                            {
                                                objDeliveryVO.ScheduleDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.SCHEDULEDATE_FLD].ToString());
                                            }
                                            catch
                                            { }
                                            lists.Add(objDeliveryVO);
                                        }
                                        catch
                                        { }
                                    }
                                }
                                catch
                                { }
                            }
                            else
                            {
                                try
                                {
                                    SO_DeliverySchedule objDeliveryVO = new SO_DeliverySchedule();
                                    objDeliveryVO.DeliveryNo = null;
                                    try
                                    {
                                        objDeliveryVO.DeliveryQuantity = Convert.ToDecimal(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.DELIVERYQUANTITY_FLD].ToString());
                                    }
                                    catch
                                    { }
                                    objDeliveryVO.DeliveryScheduleID = 0;
                                    try
                                    {
                                        objDeliveryVO.GateID = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.GATEID_FLD].ToString());
                                    }
                                    catch
                                    { }
                                    objDeliveryVO.Line = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.LINE_FLD].ToString());
                                    try
                                    {
                                        objDeliveryVO.PromiseDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.PROMISEDATE_FLD].ToString());
                                    }
                                    catch
                                    { }
                                    try
                                    {
                                        objDeliveryVO.RequiredDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.REQUIREDDATE_FLD].ToString());
                                    }
                                    catch
                                    { }
                                    objDeliveryVO.SaleOrderDetailID = Convert.ToInt32(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.SALEORDERDETAILID_FLD].ToString());
                                    try
                                    {
                                        objDeliveryVO.ScheduleDate = Convert.ToDateTime(dstData.Tables[0].Rows[i][SO_DeliveryScheduleTable.SCHEDULEDATE_FLD].ToString());
                                    }
                                    catch
                                    { }
                                    lists.Add(objDeliveryVO);
                                }
                                catch
                                { }
                            }
                            
                        }
                        catch
                        { }
                    }
                    //dcPCS.SO_DeliverySchedules.DeleteAllOnSubmit(listsDelete);
                    dcPCS.SO_DeliverySchedules.InsertAllOnSubmit(lists);
                    dcPCS.SubmitChanges();
                    trans.Complete();

                }
            }
            catch (SqlException ex)
            {
                if (ex.Errors.Count > 1)
                {
                    if (ex.Number == ErrorCode.SQLDUPLICATE_KEYCODE)
                        throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
                    throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                }
                throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
            }
        }


		//[AutoComplete(true)]
		public void UpdateDeliveryDataSet(DataSet dstData, int pintSaleOrderDetailID)
		{
			// TODO:  Add SODeliveryScheduleBO.UpdateDataSet implementation
			try
			{
				SO_DeliveryScheduleDS objSO_DeliveryScheduleDS = new SO_DeliveryScheduleDS();
				objSO_DeliveryScheduleDS.UpdateDeliveryDataSet(dstData,pintSaleOrderDetailID);

			}			
			catch (PCSDBException ex) 
			{
				throw ex;
			}
			catch (Exception ex) 
			{
				throw ex;
			}

		}

		//[AutoComplete(true)]
		public void Update(object pObjectDetail)
		{
			// TODO:  Add SODeliveryScheduleBO.Update implementation
		}

		//[AutoComplete(true)]
		public void Delete(object pObjectVO)
		{
			// TODO:  Add SODeliveryScheduleBO.Delete implementation
		}


		//[AutoComplete(true)]
		public void Add(object pObjectDetail)
		{
			// TODO:  Add SODeliveryScheduleBO.Add implementation
		}

		//[AutoComplete(true)]
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add SODeliveryScheduleBO.GetObjectVO implementation
			return null;
		}

		#endregion

		#region ISODeliveryScheduleBO Members

		//**************************************************************************              
		///    <Description>
		///       Get the Delivery Detail dataset for a specific Sale Order Detail ID
		///    </Description>
		///    <Inputs>
		///        Sale Order Detail ID
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, February 01, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet GetDeliverySchedule(int pintSaleOrderDetailID)
		{
			// TODO:  Add SODeliveryScheduleBO.GetDeliverySchedule implementation
			try 
			{
				SO_DeliveryScheduleDS objSO_DeliveryScheduleDS = new SO_DeliveryScheduleDS();
				DataSet dsData = objSO_DeliveryScheduleDS.GetDeliverySchedule(pintSaleOrderDetailID);
//				dsData.Tables[0].Columns[SO_DeliveryScheduleTable.LINE_FLD].AutoIncrement = true;
//				dsData.Tables[0].Columns[SO_DeliveryScheduleTable.LINE_FLD].AutoIncrementSeed = objSO_DeliveryScheduleDS.GetMaxDeliveryScheduleLine(pintSaleOrderDetailID) + 1;
				return dsData;
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       Get max sale order line 
		///       This max number will be used to add next value for the Line
		///    </Description>
		///    <Inputs>
		///        Sale Order Detail ID
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, February 01, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		//[AutoComplete(true)]
		public int GetMaxDeliveryScheduleLine(int pintSaleOrderDetailID)
		{
			try 
			{
				SO_DeliveryScheduleDS objSO_DeliveryScheduleDS = new SO_DeliveryScheduleDS();
				return objSO_DeliveryScheduleDS.GetMaxDeliveryScheduleLine(pintSaleOrderDetailID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

        /// <summary>
        /// Delete Delivery schedure
        /// </summary>
        /// <param name="pintSaleOrderDetailID"></param>
        public void Delete(int pintSaleOrderDetailID)
        {
            const string METHOD_NAME = "" + ".Update()";
            try
            {
                using (var trans = new TransactionScope())
                {
                    var dcPCS = new PCSDataContext(Utils.Instance.ConnectionString);
                    
                    dcPCS.DeleteDeliverySchedure(pintSaleOrderDetailID);
                   
                    dcPCS.SubmitChanges();
                    trans.Complete();

                }
            }
            catch (SqlException ex)
            {
                if (ex.Errors.Count > 1)
                {
                    if (ex.Number == ErrorCode.SQLDUPLICATE_KEYCODE)
                        throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
                    throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                }
                throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
            }
        }
        public void DeleteRow(int iId)
        {
            const string METHOD_NAME = "" + ".DeleteRow()";
            try
            {
                using (var trans = new TransactionScope())
                {
                    var dcPCS = new PCSDataContext(Utils.Instance.ConnectionString);

                    SO_DeliverySchedule objDelete = dcPCS.SO_DeliverySchedules.SingleOrDefault(e => e.DeliveryScheduleID ==iId);
                    if (objDelete != null)
                    {
                        dcPCS.SO_DeliverySchedules.DeleteOnSubmit(objDelete);
                    }

                    dcPCS.SubmitChanges();
                    trans.Complete();

                }
            }
            catch (SqlException ex)
            {
                if (ex.Errors.Count > 1)
                {
                    if (ex.Number == ErrorCode.SQLDUPLICATE_KEYCODE)
                        throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
                    throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                }
                throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
            }
        }
		//[AutoComplete(true)]
		public void DeleteDeliveryDetail (int pintSaleOrderDetailID)
		{
			try
			{
				SO_DeliveryScheduleDS objSO_DeliveryScheduleDS = new SO_DeliveryScheduleDS();
				objSO_DeliveryScheduleDS.DeleteDeliveryDetail(pintSaleOrderDetailID);

			}
			catch (PCSDBException ex) 
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion
	}
}
